﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RM2012Service.DataService;
using System.Data;
using System.Data.SqlClient;
using RM2012Service.Business;

namespace RM2012Service.DataAccess
{
    public class UserGroupDAO
    {
        DBConnection db;

        public UserGroupDAO()
        {
            db = new DBConnection();
        }

        public DataTable GetAllGroup()
        {
            return db.ExecuteDataTable("GetAllGroup", null);
        }

        public DataTable GetGroupById(string groupId)
        {
            SqlParameter param = new SqlParameter("@UserGroupId", SqlDbType.VarChar, 4);
            param.Value = groupId;
            return db.ExecuteDataTable("GetGroupById", param);
        }


        public DataTable GetGroupByLikeId(string groupId)
        {
            SqlParameter param = new SqlParameter("@UserGroupId", SqlDbType.VarChar, 4);
            param.Value = groupId;
            return db.ExecuteDataTable("GetGroupByLikeId", param);
        }

        public DataTable GetGroupByName(string groupName)
        {
            SqlParameter param = new SqlParameter("@UserGroupName", SqlDbType.NVarChar, 100);
            param.Value = groupName;
            return db.ExecuteDataTable("GetGroupByName", param);
        }

        public DataTable GetGroupByUserId(string userId)
        {
            SqlParameter param = new SqlParameter("@UserId", SqlDbType.VarChar, 6);
            param.Value = userId;
            return db.ExecuteDataTable("GetGroupByUserId", param);
        }
        
        public int AddGroup(UserGroupInfo info)
        {
            SqlParameter[] param = new SqlParameter[9];
            param[0] = new SqlParameter("@UserGroupId", SqlDbType.VarChar, 4);
            param[0].Value = info.UserGroupId;

            param[1] = new SqlParameter("@UserGroupName", SqlDbType.NVarChar, 100);
            param[1].Value = info.UserGroupName;

            param[2] = new SqlParameter("@IsDefault", SqlDbType.Bit);
            param[2].Value = info.IsDefault;

            param[3] = new SqlParameter("@IsAdmin", SqlDbType.Bit);
            param[3].Value = info.IsAdmin;

            param[4] = new SqlParameter("@IsMod", SqlDbType.Bit);
            param[4].Value = info.IsMod;

            param[5] = new SqlParameter("@CanDelete", SqlDbType.Bit);
            param[5].Value = info.CanDelete;

            param[6] = new SqlParameter("@CanReport", SqlDbType.Bit);
            param[6].Value = info.CanReport;

            param[7] = new SqlParameter("@CanEdit", SqlDbType.Bit);
            param[7].Value = info.CanEdit;

            param[8] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[8].Value = info.Remark;

            return db.ExecuteNoneQuery("AddGroup", param);
        }

        public int UpdateGroup(UserGroupInfo info)
        {
            SqlParameter[] param = new SqlParameter[9];
            param[0] = new SqlParameter("@UserGroupId", SqlDbType.VarChar, 4);
            param[0].Value = info.UserGroupId;

            param[1] = new SqlParameter("@UserGroupName", SqlDbType.NVarChar, 100);
            param[1].Value = info.UserGroupName;

            param[2] = new SqlParameter("@IsDefault", SqlDbType.Bit);
            param[2].Value = info.IsDefault;

            param[3] = new SqlParameter("@IsAdmin", SqlDbType.Bit);
            param[3].Value = info.IsAdmin;

            param[4] = new SqlParameter("@IsMod", SqlDbType.Bit);
            param[4].Value = info.IsMod;

            param[5] = new SqlParameter("@CanDelete", SqlDbType.Bit);
            param[5].Value = info.CanDelete;

            param[6] = new SqlParameter("@CanReport", SqlDbType.Bit);
            param[6].Value = info.CanReport;

            param[7] = new SqlParameter("@CanEdit", SqlDbType.Bit);
            param[7].Value = info.CanEdit;

            param[8] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[8].Value = info.Remark;

            return db.ExecuteNoneQuery("UpdateGroup", param);
        }

        public int DeleteGroup(string groupId)
        {
            SqlParameter param = new SqlParameter("@UserGroupId", SqlDbType.VarChar, 4);
            param.Value = groupId;

            return db.ExecuteNoneQuery("DeleteGroup", param);
        }
    }
}
